<script>
    var DetectRTC = {};

    var screenCallback;

    DetectRTC.screen = {
        chromeMediaSource: 'screen',
        getSourceId: function(callback) {
            screenCallback = callback;
            window.postMessage('get-sourceId', '*');
        },
        onMessageCallback: function(data) {
            // "cancel" button is clicked
            if (data == 'PermissionDeniedError') {
                DetectRTC.screen.chromeMediaSource = 'PermissionDeniedError';
                if (screenCallback) return screenCallback('PermissionDeniedError');
                else throw new Error('PermissionDeniedError');
            }

            // extension notified his presence
            if (data == 'rtcmulticonnection-extension-loaded') {
                DetectRTC.screen.chromeMediaSource = 'desktop';
            }

            // extension shared temp sourceId
            if (data.sourceId) {
                DetectRTC.screen.sourceId = data.sourceId;
                if (screenCallback) screenCallback(DetectRTC.screen.sourceId);
            }
        },
        getChromeExtensionStatus: function(callback) {
            if (!DetectRTC.screen) DetectRTC.screen = {};
            if (!DetectRTC.screen.chromeMediaSource) DetectRTC.screen.chromeMediaSource = '';

            if (DetectRTC.screen.chromeMediaSource === 'desktop') {
                callback('installed-enabled');
                return;
            }

            DetectRTC.screen.chromeMediaSource = 'screen';
            window.postMessage('are-you-there', '*');
            setTimeout(function() {
                if (DetectRTC.screen.chromeMediaSource == 'screen') {
                    callback('installed-disabled');
                } else {
                    callback('installed-enabled');
                }
            }, 2000);
        }
    };

    window.addEventListener('message', function(event) {
        if (!event.data || !(typeof event.data == 'string' || event.data.sourceId || event.data.captureSourceId || event.data.getChromeExtensionStatus)) return;

        if (event.data.getChromeExtensionStatus) {
            DetectRTC.screen.getChromeExtensionStatus(function(status) {
                window.parent.postMessage({
                    chromeExtensionStatus: status
                }, '*');
            });
            return;
        }

        if (event.data.captureSourceId) captureSourceId();

        DetectRTC.screen.onMessageCallback(event.data);
    });

    function captureSourceId() {
        // check if desktop-capture extension installed.
        DetectRTC.screen.getChromeExtensionStatus(function(status) {
            if (status != 'installed-enabled') {
                window.parent.postMessage({
                    chromeExtensionStatus: status
                }, '*');
                return;
            }

            DetectRTC.screen.getSourceId(function(sourceId) {
                window.parent.postMessage({
                    chromeMediaSourceId: sourceId
                }, '*');
            });
        });
    }
</script>